using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic.Audit;
using NopSolutions.NopCommerce.BusinessLogic.Localization;
using NopSolutions.NopCommerce.BusinessLogic.Regions;
using NopSolutions.NopCommerce.BusinessLogic.Media;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Templates;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Administration.Modules;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class RegionDetailsControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Region Region = this.RegionService.GetRegionById(this.RegionId);
                if (Region != null)
                {
                    lblTitle.Text = Server.HtmlEncode(Region.Name);
                }
                this.SelectTab(this.RegionTabs, this.TabId);
            }
        }

        protected Region Save()
        {
            Region Region = ctrlRegionInfo.SaveInfo();
            ctrlRegionSEO.SaveInfo();
            ctrlRegionProducts.SaveInfo();

            this.CustomerActivityService.InsertActivity(
                "EditRegion",
                GetLocaleResourceString("ActivityLog.EditRegion"),
                Region.Name);

            return Region;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Region Region = Save();
                    Response.Redirect("Regions.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Region Region = Save();
                    Response.Redirect(string.Format("RegionDetails.aspx?RegionID={0}&TabID={1}", Region.RegionID, this.GetActiveTabId(this.RegionTabs)));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                Region Region = this.RegionService.GetRegionById(this.RegionId);
                if (Region != null)
                {
                    this.RegionService.MarkRegionAsDeleted(Region.RegionID);

                    this.CustomerActivityService.InsertActivity(
                        "DeleteRegion",
                        GetLocaleResourceString("ActivityLog.DeleteRegion"),
                        Region.Name);
                }
                Response.Redirect("Regions.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            Region Region = this.RegionService.GetRegionById(this.RegionId);
            if (Region != null)
            {
                PreviewButton.OnClientClick = string.Format("javascript:OpenWindow('{0}', 800, 600, true); return false;", SEOHelper.GetRegionUrl(Region.RegionID));
            }

            base.OnPreRender(e);
        }

        public int RegionId
        {
            get
            {
                return CommonHelper.QueryStringInt("RegionId");
            }
        }

        protected string TabId
        {
            get
            {
                return CommonHelper.QueryString("TabId");
            }
        }
    }
}